﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AviagePortal.Entity
{
    class SiteDataOperator
    {
        static string siteDataListName = "SiteData";
        public static void ApproveAll()
        {
            SPList siteData = SPContext.Current.Web.Lists[siteDataListName];

            SPQuery query = new SPQuery();
            query.Query = "<Where><IsNotNull> <FieldRef Name='File_x0020_Size' /></IsNotNull></Where>";
            query.ViewFields = " <FieldRef Name='ID' />";
            query.ViewAttributes = "Scope='RecursiveAll'";
            SPListItemCollection items = siteData.GetItems(query);
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            foreach (SPListItem item in items)
            {
               int majorVersion = item.File.MajorVersion;
               SPFileVersion version = item.File.Versions.GetVersionFromID(majorVersion);              
               SPModerationInformation moderationInformation = item.ModerationInformation;
               if (moderationInformation.Status == SPModerationStatusType.Pending)
               {
                   item.File.Approve("ApproveAll");                   
               }
            }
        }

        public static void CancelAll()
        {
            SPWeb web = SPContext.Current.Web;
            string currentUser = web.CurrentUser.Name;
            int currentUserId = web.CurrentUser.ID;
            string siteUrl = web.Site.Url;
            string webUrl = web.ServerRelativeUrl;
            SPSecurity.RunWithElevatedPrivileges(delegate()
                 {
                     using (SPSite site = new SPSite(siteUrl))
                     {
                         SPWeb currentWeb = site.OpenWeb(webUrl);
                         currentWeb.AllowUnsafeUpdates = true;
                         SPList siteData = currentWeb.Lists[siteDataListName];
                         SPQuery query = new SPQuery();
                         query.Query = "<Where><IsNotNull> <FieldRef Name='File_x0020_Size' /></IsNotNull></Where>";
                         query.ViewFields = " <FieldRef Name='ID' />";
                         query.ViewAttributes = "Scope='RecursiveAll'";

                         SPListItemCollection items = siteData.GetItems(query);
                         foreach (SPListItem item in items)
                         {
                             SPModerationInformation moderationInformation = item.ModerationInformation;
                             if (moderationInformation.Status == SPModerationStatusType.Pending)
                             {
                                 if (item.File.Versions.Count >= 1)
                                 {
                                     string label = GetLastVersionId(item.File.Versions);                                    
                                     if (label != "-1")
                                     {
                                         SPFileVersion majorVersion = item.File.Versions.GetVersionFromLabel(label);
                                         string lastVersionId = GetLastApproveVersionId(item.File.Versions);
                                         if (lastVersionId != "-1")
                                         {
                                             item.File.Versions.RestoreByLabel(lastVersionId);
                                             majorVersion.Delete();
                                             item.File.Approve("Recovery from version:" + lastVersionId);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 });
        }

        public static void SubmitAll()
        {     
            SPContext currentContext = SPContext.Current;
            SPWeb web = SPContext.Current.Web;
            string[] paths = { "/English/PC/", "/Chinese/PC/", "/English/Mobile/", "/Chinese/Mobile/" };           
             web.AllowUnsafeUpdates = true;

                JsonGenerator.GenerateJsonForGlobalSettingsItem(web, paths);
                JsonGenerator.GenerateJsonForHomePageItem(web, paths);
                JsonGenerator.GenerateJsonForProduct(web, paths);
                JsonGenerator.GenerateJsonForCompanyHistory(web, paths);
                JsonGenerator.GenerateJsonForCompanyIntroduce(web, paths);
                JsonGenerator.GenerateJsonForContactUs(web, paths);
                JsonGenerator.GenerateJsonForLeaderShip(web, paths);
                JsonGenerator.GenerateJsonForPartnerShip(web, paths);
                JsonGenerator.GenerateJsonForSocialDuty(web, paths);

                JsonGenerator.GenerateJsonForCareer(web, paths);
                JsonGenerator.GenerateJsonForOpportunities(web, paths);
                JsonGenerator.GenerateJsonForOurLife(web, paths);
                JsonGenerator.GenerateJsonForTraining(web, paths);

                // GenerateJsonForNewsMain,GenerateJsonForMediaCenter,GenerateJsonForService
                JsonGenerator.GenerateJsonForNewsMain(web, paths);
                JsonGenerator.GenerateJsonForMediaCenter(web, paths);
                JsonGenerator.GenerateJsonForService(web, paths);
                JsonGenerator.GenerateJsonForTerms(web, paths);
                JsonGenerator.GenerateJsonForSiteMap(web, paths);
                JsonGenerator.GenerateJsonForContactInformationPage(web, paths);
        }

        public static string GetLastApproveVersionId(SPFileVersionCollection versions)
        {
            float lastVersion = 0;
            string lastLabel ="-1";
            foreach(SPFileVersion verion in versions )
            {
                if (verion.Level == SPFileLevel.Published && float.Parse(verion.VersionLabel) > lastVersion)
                {
                    lastVersion = float.Parse(verion.VersionLabel);
                    lastLabel = verion.VersionLabel;
                }
            }

            return lastLabel;
        }


        public static string GetLastVersionId(SPFileVersionCollection versions)
        {
            float lastVersion = 0;
            string lastLabel = "-1";
            foreach (SPFileVersion verion in versions)
            {
                if (float.Parse(verion.VersionLabel) > lastVersion)
                {
                    lastVersion = float.Parse(verion.VersionLabel);
                    lastLabel = verion.VersionLabel;
                }
            }

            return lastLabel;
        }
    }
}
